package com.wangyadong.hobby.thread.lesson7withconcurrent;

import java.util.concurrent.TimeUnit;

public class InterruptThread extends Thread {

    @Override
    public void run() {

        // while (!isInterrupted()) {
        //interrupted
//        boolean flag = false;
//        try {
//            while (!isInterrupted()) {
//                TimeUnit.SECONDS.sleep(1);
//                System.out.println(Thread.currentThread().getName() + "======" + Thread.currentThread().getState().name());
//            }
//        } catch (Exception e) {
////            e.printStackTrace();
//            System.out.println(Thread.currentThread().getName() + " error======" + Thread.currentThread().getState().name());
//            System.out.println("isInterrupted->" + this.isInterrupted());
//            System.out.println(Thread.currentThread().getName() + "this.isInterrupted->" + this.isInterrupted());
//            flag = true;
//            Thread.currentThread().interrupt();
//        }
//        finally {
//            if (flag)
//                Thread.currentThread().interrupt();
//        }
//        if (flag)
//                Thread.currentThread().interrupt();
        try {
            for (int i = 0; i < 100; i++) {
//                System.out.println("-->" + i + " " + Thread.interrupted());
                TimeUnit.MILLISECONDS.sleep(100);


            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("-->" + "error" + " " + Thread.interrupted());
//            this.interrupt();
        }


    }

    public static void main(String[] args) throws InterruptedException {
        InterruptThread t1 = new InterruptThread();
//        InterruptThread t3 = new InterruptThread();
//        InterruptThread t2 = new InterruptThread();
//        Thread t4 = new Thread();
//        t1.start();
//        t2.start();
        t1.start();

//        thread.start();
//
//        System.out.println("before Thread.interrupted()====" + Thread.interrupted());
//        thread.interrupt();
        System.out.println(t1.getName() + " before====" + t1.isInterrupted());
        t1.interrupt();
        System.out.println("after====" + t1.isInterrupted());
        Thread.sleep(2000);
        System.out.println("after interrupted====" + t1.interrupted());
//        t3.interrupt();
//        t4.interrupt();

//        while (t1.isInterrupted()) {
//            throw new RuntimeException();
//        }


        boolean b = Thread.holdsLock(t1);
        System.out.println("--->" + b);

    }
}
